// pages/verifyresult/verifyresult.js
const host = require('../../config.js').host;
const network = require('../../utils/network.js');
Page({

	/**
	 * 
	 */
	 onShareAppMessage() {
    return {
      title: 'radio',
      path: 'page/component/pages/radio/radio'
    }
  },

	/**
	 * 页面的初始数据
	 */
	data: {
		//奖品详细信息
		info: {},
		//优惠券id
		giftId: '',
		//顾客奖品id
		id: '',
		//优惠券列表
		goodsDeductionList: [],
		//接收到的二维码值
		result: 0,
		//分页
		goodsPage: {
			pageSize: 3,
			pageNum: 0,
			inStock: 1,// 表示库存大于0 的 抵扣券// 
			isSell:0,// 查询已经上架( 发放中[未收回])的 代金券
		},
		isLoda: true,
		//单选
		items:[
			{value: '', name: ''},
		],
		isVerify:0,// 是否可以确认核销  0 否 1 是 
		verifyMsg:'', // 扫码核销的消息
		checkedDeductionId:"",// 选中的抵扣券 id
		checked:false // 单选框选中

	},

	/**
	 * 奖品详细信息
	 */
	prizeInfo() {

		let resultCode = this.data.result;
		 
		if("undefined" == typeof(resultCode)  ){
			return;
		}
 
		network.requestGet("/xcx/customer-prize/getPrizeQRcodeDataByid/" + resultCode, {
			//二维码扫描到的奖品Id
		}, res => {
			if (res.data.code == '200') {
				let resData  = res.data.data;
				let returnCode = resData.code;// 
				if(200 != returnCode){ //  200 可以核销 其他不行
					this.setData({
						verifyMsg:resData.msg,
					})
				}else{
					this.setData({
						isVerify :1,
					})
				}

				this.setData({
					info: res.data.data.PrizeQRcodeData
				})
				
			} else {
				wx.showToast({
					title: res.data.msg,
					icon: 'none'
				})
			}
		});
	},
	/**
	 * 单选
	 */
	radioChange(e) {
		console.log('radio发生change事件，选中的抵扣券ID：', e.detail.value)
		let checkedDeductionId = e.detail.value
		return;
		// let checkedState = e.detail.
		this.setData({
			checkedDeductionId:checkedDeductionId,
		})
		
	},
	  //  // 点击 选择事件
		 checked: function(e) {

			let goodsDeductionList = this.data.goodsDeductionList;
			let i = e.currentTarget.dataset.index;

			let flag = false;
			goodsDeductionList.forEach((item,index)=>{
				if(index != i) {
					item.checked = false;
				}
			})
			goodsDeductionList[i].checked = !goodsDeductionList[i].checked;

			goodsDeductionList.forEach((item,index)=>{
				if(item.checked == true) {
					flag = true;
				}
			})
			this.setData({
				goodsDeductionList: goodsDeductionList,
				checkedDeductionId: flag? goodsDeductionList[i].goodsId:""
			})
			console.log("checkedDeductionId", this.data.checkedDeductionId);
		},

	/**
	 * 获取抵扣券列表
	 */
	getGoodsDeductionList() {

		let goodsPage = this.data.goodsPage;
		goodsPage.pageNum = goodsPage.pageNum + 1;
		let goodsDeductionList = this.data.goodsDeductionList;
		network.requestGet("/xcx/goods/deduction-list", goodsPage, res => {
			if (res.data.rows < this.data.goodsPage.pageSize) {
				this.setData({
					isLoda: false
				})
			}
			let arr = res.data.rows;
			arr.forEach((item,index)=>{
				if(0 == index && 1 == goodsPage.pageNum){ // 默认选择第一个
					item.checked = true;
					this.setData({
						checkedDeductionId: item.goodsId,
					})
				}else{
					item.checked = false;
				}
			})

			this.setData({

				goodsDeductionList: goodsDeductionList.concat(arr)
			})

		});

	},

	/**
	 * 确认核销按钮
	 */
	confirmWriteOff() {
		//确认核销拟态弹窗
		let that = this;
		let  cc = that.data.checkedDeductionId;
		console.log("发放的代金券",cc);
		// return;
 
		wx.showModal({
			title: '提示',
			content: '确认核销',
			success (res) {
				if (res.confirm) {
					console.log('用户点击确认核销')
					let id= that.data.result;
					network.requestPost("/xcx/customer-prize/scanCodePrizeVerify", {
						"id": that.data.result,
						"giftId": that.data.checkedDeductionId,	// 获取 选中的 抵扣券ID 
					}, res => {			
					
						
						wx.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						if (res.data.code == 200) {
								 // 核销成功 // 跳转 核销记录list 
								 setTimeout(function () {
									 //   switchTab  navigateTo
									wx.switchTab({
									url: '/pages/verify/verify' ,
								});
								}, 1200)
						} else {
						
						}
					});

				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
		
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		// 生命周期函数--监听页面加载
		// 获取扫码到 的 奖品id
		let resultCode = options.customerPrizeId;
		if(undefined == typeof(resultCode)){
			// 如果扫码到 undefined --- 
			resultCode = 0;
		}

		this.setData({
			result: resultCode,

		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		//奖品详细信息
		this.prizeInfo();
		// 获取抵扣券列表
		this.getGoodsDeductionList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		if (this.data.isLoda) {
			this.getGoodsDeductionList()
		};

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})